package com.fz.us.admin.company.service.impl;

import com.fz.us.admin.base.dao.BaseDao;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.company.dao.CompanyDao;
import com.fz.us.admin.company.dto.CompanyDto;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.service.CompanyService;
import com.fz.us.admin.company.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Service实现类 - 企业
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-08-02 。
 * ============================================================================
 */
@Service
public class CompanyServiceImpl extends BaseServiceImpl<Company, String> implements CompanyService {

    @Resource
    private CompanyDao companyDao;
    @Override
    public BaseDao<Company, String> getBaseDao() {
        return companyDao;
    }

    @Resource
    private UserService userService;

    @Override
    public Company createCompany(String name, User user) {
        Company company = new Company();
        company.setName(name);
        company.setOwner(user);

        save(company);

        user.setCompany(company);
        userService.update(user);

        return company;
    }

    @Override
    public CompanyDto entityToDto(Company company) {
        CompanyDto companyDto = new CompanyDto();
        companyDto.id = StringUtils.isEmpty(company.getId())?"":company.getId();
        companyDto.name = StringUtils.isEmpty(company.getName())?"":company.getName();
        companyDto.subName = StringUtils.isEmpty(company.getSubName())?"":company.getSubName();

        if (company.getOwner() == null) {
            companyDto.ownerId = "";
            companyDto.ownerName = "";
        } else {
            User owner = company.getOwner();
            companyDto.ownerId = owner.getId();
            companyDto.ownerName = owner.getName();
        }

        companyDto.province = StringUtils.isEmpty(company.getProvince())?"":company.getProvince();
        companyDto.city = StringUtils.isEmpty(company.getCity())?"":company.getCity();
        companyDto.county = StringUtils.isEmpty(company.getCounty())?"":company.getCounty();
        companyDto.address = StringUtils.isEmpty(company.getAddress())?"":company.getAddress();
        companyDto.description = StringUtils.isEmpty(company.getDescription())?"":company.getDescription();

        return companyDto;
    }

}
